import 'package:flutter/material.dart';
import 'customkbbtn.dart';

class MyKeyboard extends StatefulWidget {
  final callback;
   final String nowdata;


  @override
  State<StatefulWidget> createState() {
    return new MyKeyboardStat(this.nowdata);
  }
  MyKeyboard(this.callback,this.nowdata);
}

class MyKeyboardStat extends State<MyKeyboard> {
  bool isnext = false;
  String keydown;
  MyKeyboardStat(this.keydown);
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  var sheng = [
    '粤',
    '京',
    '冀',
    '沪',
    '津',
    '晋',
    '蒙',
    '辽',
    '吉',
    '黑',
    '苏',
    '浙',
    '皖',
    '闽',
    '赣',
    '鲁',
    '豫',
    '鄂',
    '湘',
    '桂',
    '琼',
    '渝',
    '川',
    '贵',
    '云',
    '藏',
    '陕',
    '甘',
    '青',
    '宁'
  ];
  var shuzi = [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '0',
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'i',
    'J',
    'K',
    'L',
    'M',
    'N',
    '0',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z'
  ];

  /// 定义 确定 按钮 接口  暴露给调用方
  ///回调函数执行体
  var backMethod;

  void onCommitChange() {
    widget.callback(new KeyEvent("commit"));
  }

  void onOneChange(BuildContext cont, int index) {
    if (isnext == false) {
      setState(() {
        isnext = true;
      });
      widget.callback(new KeyEvent(sheng[index]));
      keydown+=sheng[index];
    } else {
      widget.callback(new KeyEvent(shuzi[index]));
      keydown += shuzi[index];
    }
    setState(() {
      keydown = keydown;
    });
  }

  void onTwoChange(BuildContext cont) {
    widget.callback(new KeyEvent("2"));
  }

  void onThreeChange(BuildContext cont) {
    widget.callback(new KeyEvent("3"));
  }

  void onFourChange(BuildContext cont) {
    widget.callback(new KeyEvent("4"));
  }

  void onFiveChange(BuildContext cont) {
    widget.callback(new KeyEvent("5"));
  }

  void onSixChange(BuildContext cont) {
    widget.callback(new KeyEvent("6"));
  }

  void onSevenChange(BuildContext cont) {
    widget.callback(new KeyEvent("7"));
  }

  void onEightChange(BuildContext cont) {
    widget.callback(new KeyEvent("8"));
  }

  void onNineChange(BuildContext cont) {
    widget.callback(new KeyEvent("9"));
  }

  void onZeroChange(BuildContext cont) {
    widget.callback(new KeyEvent("0"));
  }

  /// 点击删除
  void onDeleteChange() {
    print('-----删除-----：'+keydown);
    keydown = keydown.substring(0, keydown.length - 1);

    if(keydown.length==0){
      setState(() {
        isnext = false;
      });
    }
    widget.callback(new KeyEvent("del"));
  }

  //下一步
  void next() {
    print('下一步');
    setState(() {
      isnext = true;
    });
  }

  void up() {
    setState(() {
      isnext = false;
    });
  }

  void finish() {}

  Widget layoutshuzi() {
    return new Container(
      key: _scaffoldKey,
      width: double.infinity,
      height: 270.0,
      color: Colors.white,
      child: new Column(
        children: <Widget>[
          new Container(
            height: 30.0,
            color: Colors.white,
            alignment: Alignment.center,
            child: new Text(
              '下滑隐藏',
              style: new TextStyle(fontSize: 12.0, color: Color(0xff999999)),
            ),
          ),
          new Container(
            height: 40.0,
            color: Colors.white,
            padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
            child: new Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Container(
                  width: MediaQuery.of(context).size.width / 3,
                  alignment: Alignment.centerLeft,
                  child: new FlatButton(
                    onPressed: up,
                    child: Text(
                      "上一步",
                      style: new TextStyle(fontSize: 16),
                    ),
                  ),
                ),
                new Container(
                  width: MediaQuery.of(context).size.width / 3,
                  alignment: Alignment.center,
                  child: new FlatButton(
                    onPressed: onCommitChange,
                    child: Text(
                      "完成",
                      style: new TextStyle(fontSize: 16),
                    ),
                  ),
                ),
                new Container(
                  width: MediaQuery.of(context).size.width / 3,
                  alignment: Alignment.centerRight,
                  child: new FlatButton(
                    onPressed: onDeleteChange,
                    child: Text(
                      "删除",
                      style: new TextStyle(fontSize: 16),
                    ),
                  ),
                )

              ],
            ),
//            child: new Center(
//              child: new FlatButton(
//                onPressed: up,
//                child: Text(
//                  "上一步",
//                  style: new TextStyle(fontSize: 16),
//                ),
//              ),
//            ),
          ),

          ///  键盘主体

          new Column(
            children: <Widget>[
              ///  第一行
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: '1', callback: (val) => onOneChange(context, 0)),
                  CustomKbBtn(
                      text: '2', callback: (val) => onOneChange(context, 1)),
                  CustomKbBtn(
                      text: '3', callback: (val) => onOneChange(context, 2)),
                  CustomKbBtn(
                      text: '4', callback: (val) => onOneChange(context, 3)),
                  CustomKbBtn(
                      text: '5', callback: (val) => onOneChange(context, 4)),
                  CustomKbBtn(
                      text: '6', callback: (val) => onOneChange(context, 5)),
                  CustomKbBtn(
                      text: '7', callback: (val) => onOneChange(context, 6)),
                  CustomKbBtn(
                      text: '8', callback: (val) => onOneChange(context, 7)),
                ],
              ),

              ///  第二行
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: '9', callback: (val) => onOneChange(context, 8)),
                  CustomKbBtn(
                      text: '0', callback: (val) => onOneChange(context, 9)),
                  CustomKbBtn(
                      text: 'A', callback: (val) => onOneChange(context, 10)),
                  CustomKbBtn(
                      text: 'B', callback: (val) => onOneChange(context, 11)),
                  CustomKbBtn(
                      text: 'C', callback: (val) => onOneChange(context, 12)),
                  CustomKbBtn(
                      text: 'D', callback: (val) => onOneChange(context, 13)),
                  CustomKbBtn(
                      text: 'E', callback: (val) => onOneChange(context, 14)),
                  CustomKbBtn(
                      text: 'F', callback: (val) => onOneChange(context, 15)),
                ],
              ),

              ///  第三行
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: 'G', callback: (val) => onOneChange(context, 16)),
                  CustomKbBtn(
                      text: 'H', callback: (val) => onOneChange(context, 17)),
                  CustomKbBtn(
                      text: 'I', callback: (val) => onOneChange(context, 18)),
                  CustomKbBtn(
                      text: 'J', callback: (val) => onOneChange(context, 19)),
                  CustomKbBtn(
                      text: 'K', callback: (val) => onOneChange(context, 20)),
                  CustomKbBtn(
                      text: 'L', callback: (val) => onOneChange(context, 21)),
                  CustomKbBtn(
                      text: 'M', callback: (val) => onOneChange(context, 22)),
                  CustomKbBtn(
                      text: 'N', callback: (val) => onOneChange(context, 23)),
                ],
              ),

              ///  第四行
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: 'O', callback: (val) => onOneChange(context, 24)),
                  CustomKbBtn(
                      text: 'P', callback: (val) => onOneChange(context, 25)),
                  CustomKbBtn(
                      text: 'Q', callback: (val) => onOneChange(context, 26)),
                  CustomKbBtn(
                      text: 'R', callback: (val) => onOneChange(context, 27)),
                  CustomKbBtn(
                      text: 'S', callback: (val) => onOneChange(context, 28)),
                  CustomKbBtn(
                      text: 'T', callback: (val) => onOneChange(context, 29)),
                  CustomKbBtn(
                      text: 'U', callback: (val) => onOneChange(context, 30)),
                  CustomKbBtn(
                      text: 'V', callback: (val) => onOneChange(context, 31)),
//                  CustomKbBtn(
//                      text: '0', callback: (val) => onZeroChange(context)),
//                  CustomKbBtn(text: '确定', callback: (val) => onCommitChange()),
                ],
              ),
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: 'W', callback: (val) => onOneChange(context, 32)),
                  CustomKbBtn(
                      text: 'X', callback: (val) => onOneChange(context, 33)),
                  CustomKbBtn(
                      text: 'Y', callback: (val) => onOneChange(context, 34)),
                  CustomKbBtn(
                      text: 'Z', callback: (val) => onOneChange(context, 35)),
//                  CustomKbBtn(
//                      text: 'S', callback: (val) => onOneChange(context, 28)),
//                  CustomKbBtn(
//                      text: 'T', callback: (val) => onOneChange(context, 29)),
//                  CustomKbBtn(
//                      text: 'U', callback: (val) => onOneChange(context, 30)),
//                  CustomKbBtn(
//                      text: 'V', callback: (val) => onOneChange(context, 31)),
//                  CustomKbBtn(
//                      text: '0', callback: (val) => onZeroChange(context)),
//                  CustomKbBtn(text: '确定', callback: (val) => onCommitChange()),
                ],
              ),
            ],
          )
        ],
      ),
    );
  }

  Widget layoutSheng() {
    return new Container(
      key: _scaffoldKey,
      width: double.infinity,
      height: 230.0,
      color: Colors.white,
      child: new Column(
        children: <Widget>[
          new Container(
            height: 30.0,
            color: Colors.white,
            alignment: Alignment.center,
            child: new Text(
              '下滑隐藏',
              style: new TextStyle(fontSize: 12.0, color: Color(0xff999999)),
            ),
          ),
          new Container(
            height: 40.0,
            color: Colors.white,
            padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
            child: new Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                new Container(
                  width: MediaQuery.of(context).size.width / 3,
                  alignment: Alignment.centerLeft,
                  child: new FlatButton(
                    onPressed: next,
                    child: Text(
                      "",
                      style: new TextStyle(fontSize: 16),
                    ),
                  ),
                ),
                new Container(
                  width: MediaQuery.of(context).size.width / 3,
                  alignment: Alignment.center,
                  child: new FlatButton(
                    onPressed: next,
                    child: Text(
                      "下一步",
                      style: new TextStyle(fontSize: 16),
                    ),
                  ),
                ),
                new Container(
                  width: MediaQuery.of(context).size.width / 3,
                  alignment: Alignment.centerRight,
                  child: new FlatButton(
                    onPressed: onDeleteChange,
                    child: Text(
                      "删除",
                      style: new TextStyle(fontSize: 16),
                    ),
                  ),
                )

              ],
            ),
//            child: new Row(
//              children: <Widget>[
//              Text("下一步",style: new TextStyle(fontSize: 16))
////          new Expanded(child: Text("上一步",style: new TextStyle(fontSize: 16),),),
////          new Expanded(child: Text("下一步",style: new TextStyle(fontSize: 16),),),
//
//              ],
//            ),
          ),

          ///  键盘主体

          new Column(
            children: <Widget>[
              ///  第一行
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: '粤', callback: (val) => onOneChange(context, 0)),
                  CustomKbBtn(
                      text: '京', callback: (val) => onOneChange(context, 1)),
                  CustomKbBtn(
                      text: '冀', callback: (val) => onOneChange(context, 2)),
                  CustomKbBtn(
                      text: '沪', callback: (val) => onOneChange(context, 3)),
                  CustomKbBtn(
                      text: '津', callback: (val) => onOneChange(context, 4)),
                  CustomKbBtn(
                      text: '晋', callback: (val) => onOneChange(context, 5)),
                  CustomKbBtn(
                      text: '蒙', callback: (val) => onOneChange(context, 6)),
                  CustomKbBtn(
                      text: '辽', callback: (val) => onOneChange(context, 7)),
                ],
              ),

              ///  第二行
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: '吉', callback: (val) => onOneChange(context, 8)),
                  CustomKbBtn(
                      text: '黑', callback: (val) => onOneChange(context, 9)),
                  CustomKbBtn(
                      text: '苏', callback: (val) => onOneChange(context, 10)),
                  CustomKbBtn(
                      text: '浙', callback: (val) => onOneChange(context, 11)),
                  CustomKbBtn(
                      text: '皖', callback: (val) => onOneChange(context, 12)),
                  CustomKbBtn(
                      text: '闽', callback: (val) => onOneChange(context, 13)),
                  CustomKbBtn(
                      text: '赣', callback: (val) => onOneChange(context, 14)),
                  CustomKbBtn(
                      text: '鲁', callback: (val) => onOneChange(context, 15)),
                ],
              ),

              ///  第三行
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: '豫', callback: (val) => onOneChange(context, 16)),
                  CustomKbBtn(
                      text: '鄂', callback: (val) => onOneChange(context, 17)),
                  CustomKbBtn(
                      text: '湘', callback: (val) => onOneChange(context, 18)),
                  CustomKbBtn(
                      text: '桂', callback: (val) => onOneChange(context, 19)),
                  CustomKbBtn(
                      text: '琼', callback: (val) => onOneChange(context, 20)),
                  CustomKbBtn(
                      text: '渝', callback: (val) => onOneChange(context, 21)),
                  CustomKbBtn(
                      text: '川', callback: (val) => onOneChange(context, 22)),
                  CustomKbBtn(
                      text: '贵', callback: (val) => onOneChange(context, 23)),
                ],
              ),

              ///  第四行
              new Row(
                children: <Widget>[
                  CustomKbBtn(
                      text: '云', callback: (val) => onOneChange(context, 24)),
                  CustomKbBtn(
                      text: '藏', callback: (val) => onOneChange(context, 25)),
                  CustomKbBtn(
                      text: '陕', callback: (val) => onOneChange(context, 26)),
                  CustomKbBtn(
                      text: '甘', callback: (val) => onOneChange(context, 27)),
                  CustomKbBtn(
                      text: '青', callback: (val) => onOneChange(context, 28)),
                  CustomKbBtn(
                      text: '宁', callback: (val) => onOneChange(context, 29)),

//                  CustomKbBtn(
//                      text: '0', callback: (val) => onZeroChange(context)),
//                  CustomKbBtn(text: '确定', callback: (val) => onCommitChange()),
                ],
              ),
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (isnext == false)
      return layoutSheng();
    else
      return layoutshuzi();
  }
}

class KeyEvent {
  ///  当前点击的按钮所代表的值
  String key;


  KeyEvent(this.key);

  bool isDelete() => this.key == "del";

  bool isCommit() => this.key == "commit";
}
